package com.liankuan.console.controller;

import com.google.common.collect.ImmutableMap;
import com.liankuan.console.common.BaseRsp;
import com.liankuan.console.common.ResultEnum;
import com.liankuan.console.controller.vo.AddExchangeVO;
import com.liankuan.console.dto.Exchange;
import com.liankuan.console.dto.UserRole;
import com.liankuan.console.service.ExchangeService;
import com.liankuan.console.service.ServiceImpl.UserRoleServiceImpl;
import com.liankuan.console.service.UserExchangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/exchange")
public class ExchangeController {

    private final ExchangeService exchangeService;
    private final UserExchangeService userExchangeService;
    private final UserRoleServiceImpl userRoleService;

    @Autowired
    public ExchangeController(ExchangeService exchangeService, UserExchangeService userExchangeService,
                              UserRoleServiceImpl userRoleService) {
        this.exchangeService = exchangeService;
        this.userExchangeService = userExchangeService;
        this.userRoleService = userRoleService;
    }


    /**
     * 获取交易所列表
     *
     * @return
     */
    @RequestMapping(value = "/lists", method = RequestMethod.GET)
    public BaseRsp ExchangeLists(@RequestAttribute Integer userId) {
        //首先获取该用户所拥有的交易所IDs
        List<Integer> exchangeIds = userExchangeService.findAllExchanges(userId);


        //根据id集合去获取对应的所有的交易所信息(超级用户全部返回)
        List<Exchange> list;
        UserRole userRole = userRoleService.findRoleByUserId(userId);
        if ("admin".equals(userRole.getRole())) {
            //当为admin时获取所有的交易所
            list = exchangeService.exchangeLists();
        } else {
            //否则获取该用户创建的交易所
            list = exchangeService.findAllExchangeByIds(exchangeIds);
        }

        return new BaseRsp<>(ImmutableMap.of("list", list, "total", list.size()));
    }

    /**
     * 根据ID获取交易所信息
     *
     * @param exchangeId
     * @return
     */
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public BaseRsp getExchangeById(Integer exchangeId) {
        Exchange exchange = exchangeService.findById(exchangeId);
        return new BaseRsp<>(exchange);
    }

    /**
     * 更新交易所信息
     *
     * @param exchange
     * @return
     */
    @RequestMapping(value = "/updateInfo", method = RequestMethod.POST)
    public BaseRsp updateExchangeInfo(@RequestBody Exchange exchange) {
        exchangeService.updateExchangeInfo(exchange);
        return new BaseRsp<>(ResultEnum.SUCCESS);
    }

    /**
     * 删除某个交易所信息
     *
     * @param exchangeId
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public BaseRsp deleteExchangeInfo(@RequestAttribute Integer userId, Integer exchangeId) {
        exchangeService.deleteExchangeInfo(userId, exchangeId);
        return new BaseRsp<>(ResultEnum.SUCCESS);
    }

    /**
     * 新增交易所信息
     *
     * @param exchange
     * @return
     */
    @RequestMapping(value = "/addExchange", method = RequestMethod.POST)
    public BaseRsp addExchange(@RequestBody Exchange exchange, @RequestAttribute Integer userId) {
        exchangeService.addExchange(exchange, userId);
        return new BaseRsp<>(ResultEnum.SUCCESS);
    }

    /**
     * 获取全部交易所信息(用来策略中编辑交易所+新增策略信息时使用)
     *
     * @return
     */
    @RequestMapping("/allExchange")
    public BaseRsp getAllExchange(@RequestAttribute Integer userId) {
        UserRole userRole = userRoleService.findRoleByUserId(userId);

        List<AddExchangeVO> items;
        if ("admin".equals(userRole.getRole())) {
            items = exchangeService.findAllExchange();
        } else {
            items = exchangeService.findAllExchangeByUserId(userId);
        }
        return new BaseRsp<>(items);
    }


}
